package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjw.zstu.VO.ExhibitionCompanyVO;
import com.mjw.zstu.entity.*;
import com.mjw.zstu.mapper.*;
import com.mjw.zstu.resultDto.JsonResult;
import javafx.scene.control.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@Service
public class ExhibitionService {
    @Autowired
    ExhibitonMapper exhibitonMapper;
    @Autowired
    ExhibitionCompanyMapper exhibitionCompanyMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    ExhibitorMapper exhibitorMapper;
    public JsonResult refuse(Exhibition exhibition){
        int id=exhibition.getId();
        Exhibition e=  exhibitonMapper.selectById(id);//展会详情
        long t=System.currentTimeMillis();
        Message m=new Message();
        m.setMessage("会展:"+e.getName()+"未通过审核，原因为:"+exhibition.getRefuse_reason());
        m.setReceiver(e.getCompany_id());
        m.setIs_read(0);
        m.setIs_user(0);
        m.setCreate_time(t);
        messageMapper.insert(m);
        exhibitonMapper.updateById(exhibition);
        return new JsonResult(200,"操作成功",null);
    }

    public JsonResult startExhibition(Exhibition exhibition){
        int company_id=exhibition.getCompany_id();
        Company company=new Company();
        company.setCompany_id(company_id);
        Company company1= companyMapper.selectById(company_id);
        if(company1.getIs_review()==0){
            return new JsonResult(500,"审核未通过不允许开设会展",null);
        }
        exhibition.setIs_review(0);
        exhibition.setIs_banner_recommend(0);
        exhibition.setIs_recommend(0);

        exhibitonMapper.insert(exhibition);
        return new JsonResult(200,"创建成功",null);
    }
//    public Map getDetail(int id){
//        return exhibitionCompanyMapper.getDetail(id);
//    }
//    public List<Map> getAll(Pagination pagination){
//        return exhibitionCompanyMapper.getAllExhibition(pagination);
//    }
//    public JsonResult getlist(){
//        return exhibitionCompanyMapper.getAllExhibition(0,10);
//    }
    public List<Type> getTypes(){
        QueryWrapper<Type> typeQueryWrapper=new QueryWrapper<>();
        return typeMapper.selectList(typeQueryWrapper);
    }
    public JsonResult getexhibition(int pageNum,int pageSize){
      return new JsonResult(200,"获取成功",new Page( exhibitionCompanyMapper.getAllExhibition(pageNum,pageSize),exhibitionCompanyMapper.allexhibition()));
    }
    public int reviewExhibiton(Exhibition exhibition){
        int id=exhibition.getId();
        Exhibition e=  exhibitonMapper.selectById(id);//展会详情
        long t=System.currentTimeMillis();
        Message m=new Message();
        m.setMessage("会展:"+e.getName()+"通过审核啦!!");
        m.setReceiver(e.getCompany_id());
        m.setIs_read(0);
        m.setIs_user(0);
        m.setCreate_time(t);
        messageMapper.insert(m);
        return exhibitonMapper.updateById(exhibition);
    }
    public JsonResult getexhibitionbytype(int type_id,int pageNum,int pageSize){
        return new JsonResult(200,"获取成功",new Page( exhibitionCompanyMapper.getAllExhibitionbyType(type_id,(pageNum-1)*pageSize,pageSize),exhibitionCompanyMapper.allexhibitionbyType(type_id)));
    }
    public JsonResult getexhibitionbycompany(String company,int pageNum,int pageSize){
        return new JsonResult(200,"获取成功",new Page( exhibitionCompanyMapper.getAllExhibitionbyCompany(company,(pageNum-1)*pageSize,pageSize),exhibitionCompanyMapper.allexhibitionbyCompany(company)));
    }
    public JsonResult getExhibitionByid(int id){
        Map m=exhibitionCompanyMapper.getDetail(id);
        List l=exhibitorMapper.Exhibitor(id);
        m.put("exhibitor",l);
        return new JsonResult(200,"获取成功",m);
    }
    public JsonResult getexhibitionbyCompanyID(int id){
        Exhibition exhibition=new Exhibition();
        exhibition.setCompany_id(id);
        QueryWrapper<Exhibition> e=new QueryWrapper<>(exhibition).orderByAsc("is_review");
        List l=exhibitonMapper.selectList(e);
        return new JsonResult(200,"获取成功",l);
    }
    public JsonResult updateExhibition(Exhibition exhibition){
       int l= exhibitonMapper.updateById(exhibition);
       if(l==1){
           return  new JsonResult(200,"获取成功",null);
       }
       return new JsonResult(500,"获取失败",null);
    }
    public JsonResult getTodayExhibition(){
        Calendar  calendar=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        long start=calendar.getTimeInMillis();
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        long end=calendar.getTimeInMillis();
        return new JsonResult(200,"获取成功", exhibitionCompanyMapper.getTodayExhibition(start,end));
    }
    public JsonResult CompanyExhibiton(int company_id){
        return new JsonResult(200,"获取成功", exhibitorMapper.companyExhibition(company_id));
    }
    public JsonResult deleteExhibition(int id){
        exhibitonMapper.deleteById(id);
        return new JsonResult(200,"删除成功",null);
    }
    //获取轮播图
    public JsonResult getbanner(){

        List l=exhibitonMapper.getbanner();
        return new JsonResult(200,"获取成功",l);
    }
    //获取非轮播
    public JsonResult getnotBanner(){
        List l=exhibitonMapper.getnotBanner();
        return new JsonResult(200,"获取成功",l);
    }
    //删除轮播
    public JsonResult deleteBanner(int id){
        Exhibition exhibition=new Exhibition();
        exhibition.setId(id);
        exhibition.setIs_banner_recommend(0);
        exhibitonMapper.updateById(exhibition);
        return new JsonResult(200,"操作成功",null);
    }
    //新增轮播
    public JsonResult addbanner(int[] banner){
        Exhibition exhibition=new Exhibition();
        exhibition.setIs_banner_recommend(1);
        QueryWrapper<Exhibition> exhibitionQueryWrapper=new QueryWrapper<>(exhibition);
       int length= exhibitonMapper.selectCount(exhibitionQueryWrapper);
       if(length+banner.length>5){
           return new JsonResult(500,"轮播最多设置五张",null);
       }
        for(int i=0;i<banner.length;i++){
            Exhibition exhibition1=new Exhibition();
            exhibition1.setId(banner[i]);
            exhibition1.setIs_banner_recommend(1);
            exhibitonMapper.updateById(exhibition1);
        }
        return new JsonResult(200,"新增成功",null);
    }
    //获取推荐
    public JsonResult getrecommend(){

        List l=exhibitonMapper.getrecommend();
        return new JsonResult(200,"获取成功",l);
    }
    //获取非推荐
    public JsonResult getnotRecommend(){
        List l=exhibitonMapper.gettnorecommend();
        return new JsonResult(200,"获取成功",l);
    }

    public JsonResult addrecommend(int[] banner){
        Exhibition exhibition=new Exhibition();
        exhibition.setAdmin_recommend(1);
        QueryWrapper<Exhibition> exhibitionQueryWrapper=new QueryWrapper<>(exhibition);
        int length= exhibitonMapper.selectCount(exhibitionQueryWrapper);
        Exhibition exhibition2=new Exhibition();
        exhibition2.setIs_recommend(1);
        QueryWrapper<Exhibition> exhibitionQueryWrapper2=new QueryWrapper<>(exhibition2);
        int length2= exhibitonMapper.selectCount(exhibitionQueryWrapper2);
        if(length+banner.length+length2>20){
            return new JsonResult(500,"推荐最多设置20张",null);
        }
        for(int i=0;i<banner.length;i++){
            Exhibition exhibition1=new Exhibition();
            exhibition1.setId(banner[i]);
            exhibition1.setAdmin_recommend(1);
            exhibitonMapper.updateById(exhibition1);
        }
        return new JsonResult(200,"新增成功",null);
    }
    public JsonResult deleterecommend(int id){
        Exhibition exhibition=new Exhibition();
        exhibition.setId(id);
        exhibition.setAdmin_recommend(2);
        exhibition.setIs_recommend(0);
        exhibitonMapper.updateById(exhibition);
        return new JsonResult(200,"操作成功",null);
    }


//    public List<ExhibitionCompanyVO> getlist(Page<ExhibitionCompanyVO> page){
//
//    }
}
